package utils;

import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 获取随机List<T> - 工具类
 */
public class RandomListUtils {
    static class Jcy{

        private String id;
        private String aihao;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getAihao() {
            return aihao;
        }

        public void setAihao(String aihao) {
            this.aihao = aihao;
        }

        @Override
        public String toString() {
            return "Jcy{" +
                    "id='" + id + '\'' +
                    ", aihao='" + aihao + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Jcy> sysUsers = new ArrayList<>();
        Jcy s1 = new Jcy();
        s1.setId("1");
        s1.setAihao("好1");
        Jcy s2 = new Jcy();
        s2.setId("2");
        s2.setAihao("好2");
        Jcy s3 = new Jcy();
        s3.setId("3");
        s3.setAihao("好3");
        Jcy s4 = new Jcy();
        s4.setId("4");
        s4.setAihao("好4");
        sysUsers.add(s1);
        sysUsers.add(s2);
        sysUsers.add(s3);
        sysUsers.add(s4);
        for (int i=0; i<1000; i++) {
            System.out.println(randomList(sysUsers, 3));
        }
    }
    public static <T> List<T> randomList(List<T> targetList, int randomSize){
        List<T> ret= new ArrayList<>();
        if(targetList.size()<=randomSize){
            return targetList;
        }
        if(randomSize<=0||targetList.size()==0){
            return ret;
        }
        Set<T> ts = RandomUtil.randomEleSet(targetList, randomSize);
        List<T> list = new ArrayList<T>();
        list.addAll(ts);
        return list;
//        return RandomUtil.randomEleSet(targetList, randomSize);
    }
}
